package me.zhulu.drools.wac.loan;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.commons.lang3.time.DateUtils;
import org.kie.api.KieServices;
import org.kie.api.event.rule.DebugAgendaEventListener;
import org.kie.api.event.rule.DebugRuleRuntimeEventListener;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.StatelessKieSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: herosea
 * Date: 14-7-17
 * Time: 上午10:33
 * To change this template use File | Settings | File Templates.
 */
public class CreditMain {

    private static final Logger logger = LoggerFactory.getLogger(CreditMain.class);

    private static final Logger loanALogger = LoggerFactory.getLogger("drools.loan.a");

    public static void main(String[] args) throws Exception {

//        while (true) {
            execute();
//        }
    }

    private static void execute() throws ParseException {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        Date todayTime = dateFormat.parse("20140717");
        StatelessKieSession s = new CreditMain().createLoanSession(todayTime);

        List<Result> results = new ArrayList<Result>();

        boolean hasLate = false;
        for (int i = 0; i < 4; i++) {
            int uid;
            CreditLate creditLate;
            Result result;
            CreditUse user;

            uid = 1000 + i;
            creditLate = new CreditLate();
            creditLate.setUid(uid);
            hasLate = !hasLate;
            creditLate.setLatefee_indicator(hasLate);
            creditLate.setLateRecentfee(8 + i);
            creditLate.setLateHalfYearTimes(i);

            result = new Result();
            result.setUid(uid);
            result.setSuite("A");

            user = new CreditUse();
            user.setUid(uid);
            user.setRegisterTime(DateUtils.addDays(todayTime, -29 - i));
            user.setRepayportion360(0.19F);
            user.setUsagerate1year(0.91F);


            Object[] objects = new Object[]{creditLate, result, user};

            s.execute(Arrays.asList(objects));

            results.add(result);
            logger.info(JSON.toJSONString(objects, SerializerFeature.PrettyFormat));
        }

        print(todayTime, results);
    }

    private static void print(Date today, List<Result> results) {
        System.out.println("当前时间:" + today);
        for (Result result : results) {
            System.out.println(result.getUid() + " | " + result.getSuite() + " | " + result.isRefuse());
            for (ResultItem item : result.getVeto().values()) {
                System.out.println("    " + item.getGroup() + " | " + item.getScore() + " | " + item.getRule() + " | " + item.getMessage());
            }
        }
    }

    public StatelessKieSession createLoanSession(Date todayTime) {

        KieServices ks = KieServices.Factory.get();
        KieContainer kc = ks.getKieClasspathContainer();
        StatelessKieSession ksession = kc.newStatelessKieSession("loanA_KS");

        Today today = new Today();
        today.setTime(todayTime);

        ksession.setGlobal("logger", loanALogger);
        ksession.setGlobal("today", today);

        //在配置文件里面配置了，所以注释掉
//        ksession.addEventListener(new DebugAgendaEventListener());
//        ksession.addEventListener(new DebugRuleRuntimeEventListener());

        return ksession;
    }
}
